package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthUser;
import com.tmc.util.ObjectUtil;

@Repository
public class UserDaoImpl extends BaseDaoImpl<EthUser> {

	public EthUser getUser(String userCode, String password) throws Exception {
		String sql = " from EthUser where enabledFlag = 'Y' and userCode = :userCode and password = :password";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userCode", userCode);
		params.put("password", password);
		return (EthUser) this.get(sql, params);
	}
	
	
	public EthUser getUser(ContextInfo context, EthUser cond){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
		if(cond.getUserId() != null){
			sql.append(" and c.userId = :userId");
			params.put("userId", cond.getUserId());
		}
		if(ObjectUtil.hasText(cond.getUserCode())){
			sql.append(" and c.userCode = :userCode");
			params.put("userCode", cond.getUserCode());
		}
		if(ObjectUtil.hasText(cond.getUserName())){
			sql.append(" and c.userName like :userName");
			params.put("userName", cond.getUserName());
		}
		
		return (EthUser) this.get(sql, params);
	}
	public List<EthUser> findAll(){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
	
		return this.findList(sql.toString(), params);		
	}
	public List<EthUser> findListByName(ContextInfo context, String query){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(query)){
			sql.append(" and (c.userName like :userName or c.userCode like :userCode)");
			params.put("userName", "%"+query+"%");
			params.put("userCode", "%"+query+"%");
		}
		return this.findList(sql.toString(), params);		
	}
	
	public Pagination<EthUser> findByCond(ContextInfo contextInfo, EthUser cond, Integer pageIndex, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
		if(cond.getUserId() != null){
			sql.append(" and c.userId = :userId");
			params.put("userId", cond.getUserId());
		}
		if(ObjectUtil.hasText(cond.getUserCode())){
			sql.append(" and c.userCode = :userCode");
			params.put("userCode", cond.getUserCode());
		}
		if(ObjectUtil.hasText(cond.getUserName())){
			sql.append(" and c.userName like :userName");
			params.put("userName", cond.getUserName());
		}
		if(cond.getVipLevel() != null){
			sql.append(" and c.vipLevel = :vipLevel");
			params.put("vipLevel", cond.getVipLevel());
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public EthUser getByCode(ContextInfo context, String userCode) {
		if (!ObjectUtil.hasText(userCode)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
		sql.append(" and c.userCode = :userCode");
		params.put("userCode", userCode);

		return this.get(sql.toString(), params);
	}
	
	

	public List<EthUser> findDeptManager(ContextInfo context, Long deptId) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthUser c where c.enabledFlag = 'Y' ");
		sql.append(" and c.deptId = :deptId");
		params.put("deptId", deptId);
		sql.append(" and c.vipLevel = 2 ");
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findList(sql.toString(), params);
	}

	public EthUser getByUserCode(String userCode) {
		String sql = " from EthUser where enabledFlag = 'Y' and userCode = :userCode ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userCode", userCode);
		return (EthUser) this.get(sql, params);
	}



}
